package com.hpu.callback;

import android.os.Handler;
import android.os.Message;

import com.hpu.constants.HttpConstants;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 实现了登录业务类的回调接口
 * @author Administrator
 *
 */
public class ScanCallback implements Callback{

	private Handler handler;
	
	public ScanCallback(Handler handler) {
		this.handler=handler;
	}
	
	@Override
	public void onFailure(Call arg0, IOException arg1) {
		handler.sendEmptyMessage(HttpConstants.LOGIN_ERROR);
		arg1.printStackTrace();
	}

	@Override
	public void onResponse(Call arg0, Response arg1) throws IOException {
		String result = arg1.body().string();
		if(result.equals("404")){
			handler.sendEmptyMessage(HttpConstants.VPN_TOOLONG);
		}
		else if(result.equals("501")){
			handler.sendEmptyMessage(HttpConstants.KEY_ERROR);
		}
		else if(result.equals("502")){
			handler.sendEmptyMessage(HttpConstants.NEED_INFO);
		}
		else if (result.equals("1")) {
			Message message = new Message();
			message.what = HttpConstants.SCAN_SUCCESS;
			message.obj = result;
			handler.sendMessage(message);
		}
		
		else if (result.equals("-1")) handler.sendEmptyMessage(HttpConstants.SCAN_KEY_ERROR);
		
		else{
			handler.sendEmptyMessage(HttpConstants.SCAN_ERROR);
		}
		
		
		arg1.close();
		arg1.body().close();
	}
}
